#!/bin/sh
#This is knfs/init for AM335X_RTU_0207
 
export knfshostname=U0207
 
export beep_ioidx=22
export beep_off=0
export beep_on=1
export led_ioidx=20
export led_off=1
export led_on=0
 
#挂载rtfs文件系统
mount_mtd_rtfs()
{
    #当前系统使用UBIFS文件系统
    /usr/sbin/ubiattach /dev/ubi_ctrl -m 2 -d 0
    /bin/mount -t ubifs ubi0_0 /rtfs
    return $?
}
 
#挂载vend文件系统
mount_mtd_vend()
{
    #当前系统使用UBIFS文件系统
    /usr/sbin/ubiattach /dev/ubi_ctrl -m 5 -d 3
    /bin/mount -t ubifs ubi3_0 /vend
    return $?
}

export logtag="knfs"

source /etc/colorc

#消息初始化
msginit()
{
	export logroot=/dev
	export logfile=console
	if [ ! -d ${logroot} ]; then
		mkdir -p ${logroot}
	fi
	swarn "${logtag}:################################"> ${logroot}/${logfile}
}

#消息打印输出
export msgdbg=0
msglog()
{
	if [ ! -d ${logroot} ]; then
		mkdir -p ${logroot}
	fi
	tmpstr=$*
	if [ -n "$(/bin/echo   ${tmpstr} | /bin/grep -w OK)" ]; then
		sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w START)" ]; then
		swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w ERROR)" ]; then
		serro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w FAILED)" ]; then
		perro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w DONE)" ]; then
		pdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w FINISH)" ]; then
		sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w TMO)" ]; then
		swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	else
		if [ $msgdbg -ne 0 ]; then
			if [ -n "$(/bin/echo   ${tmpstr} | /bin/grep -w ok)" ]; then
				sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w start)" ]; then
				swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w error)" ]; then
				serro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w failed)" ]; then
				perro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w done)" ]; then
				pdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w finish)" ]; then
				sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w tmo)" ]; then
				swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			else
				sinfo "${logtag}: $*" > ${logroot}/${logfile}
			fi
		fi
	fi
}

fakev_mount()
{
	if [ ! -d /ual ]; then
		mkdir -p /fakev > /dev/null 2>&1
		/bin/mount -t tmpfs tmpfs /fakev > /dev/null 2>&1
	fi
}

wait_dir_ready()
{
	CNT=10
	while true; do
		if [ -d $1 ]; then
			return 0
		fi
		CNT=`expr $CNT - 1`
		if [ $CNT -eq 0 ]; then
			return 1
		fi
		/bin/usleep 100000
	done
	return 1
}

hotplug_check_ualmnt()
{
	ret=0
	if [ ! -d /ual ]; then
		mkdir -p /ual > /dev/null 2>&1
		/bin/mount -t tmpfs tmpfs /ual
	fi
	if [ ! -d $1 ]; then
		
		if [ -e /sbin/mdev ]; then
			/bin/echo > /dev/mdev.seq
			/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
			/sbin/mdev -s
		else
			/bin/echo > /dev/mdev.seq
			/bin/echo /bin/mdev > /proc/sys/kernel/hotplug
			/bin/mdev -s
		fi
	fi
	wait_dir_ready $1
	ret=$?
	return $ret 
}

#等待块设备就绪
wait_for_blkdev_ready()
{
	CNT=10
	while true; do
		if [ -b $1 ]; then
			return 0
		fi

		msglog "Wait $1 ready [START]"

		CNT=`expr $CNT - 1`
		if [ $CNT -eq 0 ]; then
			msglog "Wait $1 ready [ERROR]"
			return 1
		fi
		/bin/usleep 600000
	done
}

notice_otaupdate_init()
{
	if [ ! -d /ioport ]; then
		mkdir /ioport
	fi
	if [ ! -z ${beep_ioidx} ]; then
		/bin/echo ${beep_ioidx} > /sys/class/gpio/export
		/bin/echo out         > /sys/class/gpio/gpio${beep_ioidx}/direction
		/bin/echo ${beep_off} > /sys/class/gpio/gpio${beep_ioidx}/value
		ln -s /sys/class/gpio/gpio${beep_ioidx} /ioport/notice_beep
		chmod 777 /ioport/notice_beep/*
	fi

	if [ ! -z ${led_ioidx} ]; then
		/bin/echo ${led_ioidx} > /sys/class/gpio/export
		/bin/echo out        > /sys/class/gpio/gpio${led_ioidx}/direction
		/bin/echo ${led_off} > /sys/class/gpio/gpio${led_ioidx}/value
		ln -s /sys/class/gpio/gpio${led_ioidx} /ioport/notice_led
		chmod 777 /ioport/notice_led/*
	fi

}

notice_otaupdate_deinit()
{
	if [ -e /ioport/notice_beep ]; then
		/bin/echo ${beep_off} > /ioport/notice_beep/value
	fi
	if [ -e /ioport/notice_led ]; then
		/bin/echo ${led_off} > /ioport/notice_led/value
	fi
	/bin/rm -rf /ioport/notice_beep > /dev/null 2>&1
	if [ -d /sys/class/gpio/gpio${beep_ioidx} ]; then
		/bin/echo ${beep_ioidx} > /sys/class/gpio/unexport
	fi

	/bin/rm -rf /ioport/notice_led > /dev/null 2>&1
	if [ -d /sys/class/gpio/gpio${led_ioidx} ]; then
		/bin/echo ${led_ioidx} > /sys/class/gpio/unexport
	fi
}

notice_otaupdate_start()
{
	notice_otaupdate_init
	if [ -e /ioport/notice_beep ]; then
		/bin/echo ${beep_on}  > /ioport/notice_beep/value
		/bin/usleep 150000
		/bin/echo ${beep_off} > /ioport/notice_beep/value
		/bin/usleep 1000000
	fi
	if [ -e /ioport/notice_led ]; then
		/bin/echo ${led_on} > /ioport/notice_led/value
	fi
	notice_otaupdate_deinit
}

notice_otaupdate_finish()
{
	notice_otaupdate_init
	export bloop=0
	if [ "x${otaimg_ftype}" = "xdata" ]; then
		export bloop=3
	elif [ "x${otaimg_ftype}" = "xroot" ]; then
		export bloop=4
	elif [ "x${otaimg_ftype}" = "xrtfs" ]; then
		export bloop=5
	elif [ "x${otaimg_ftype}" = "xknfs" ]; then
		export bloop=6
	fi
	while [ ${bloop} -ne 0 ]
	do
		/bin/echo ${beep_on}  > /ioport/notice_beep/value
		/bin/usleep 300000
		/bin/echo ${beep_off} > /ioport/notice_beep/value
		/bin/usleep 300000
		let bloop--
	done
	if [ -e /ioport/notice_led ]; then
		/bin/echo ${led_off} > /ioport/notice_led/value
	fi
	notice_otaupdate_deinit
}

notice_otaupdate_failed()
{
	notice_otaupdate_init
	if [ -e /ioport/notice_beep ]; then
		/bin/echo ${beep_on}  > /ioport/notice_beep/value
		/bin/usleep 5000000
		/bin/echo ${beep_off}  > /ioport/notice_beep/value
	fi
	if [ -e /ioport/notice_led ]; then
		/bin/echo ${led_off} > /ioport/notice_led/value
	fi
	notice_otaupdate_deinit
}

#改变OTA状态
change_obmota_sta()
{
	tmpsta=$1
	export OTA_STA_NOW=${tmpsta}
	/bin/echo "${tmpsta}" > ${otamgr_dir}/${obmota_sta_file}
	/bin/sync
	msglog "obmota state change to ${tmpsta}"
}

#检查MD5码是否一致
check_otaimg_md5()
{
	pack_md5=$(/usr/bin/md5sum ${1}/${2} |/usr/bin/awk {'print $1'})
	file_md5=$(/bin/cat ${1}/${3})
	if [ "x${pack_md5}" = "x${file_md5}" ]; then
		return 0
	fi
	return 1
}

#检查升级文件是否最新
check_otaimg_isnew()
{
	save_fpath=/vend/${2}.old
	if [ ! -f ${save_fpath} ]; then
		return 0
	fi
	save_md5=$(/bin/cat ${save_fpath})
	file_md5=$(/bin/cat ${1}/${2})
	if [ "x${save_md5}" != "x${file_md5}" ]; then
		return 0
	fi
	return 1
}

#检测固件文件是否有效
check_otaimg_isvalid()
{
	validcnt=0
	export otaimg_data_valid=0
	export otaimg_root_valid=0
	export otaimg_rtfs_valid=0

	msglog "check otaimg files in ${otaimg_dir} [start]"
	msglog "check data otaimg exist [start]"
	if [ -f ${otaimg_dir}/update_data.tar.gz ]&&[ -f ${otaimg_dir}/update_data.md5 ]; then
		#动态打开调试信息
		export msgdbg=1
		msglog "check data otaimg exist [OK]"
		#校验固件文件MD5
		msglog "check data otaimg md5sum [START]"
		check_otaimg_md5 ${otaimg_dir} update_data.tar.gz update_data.md5
		if [ $? -eq 0 ]; then
			msglog "check data otaimg md5sum [OK]"
			#校验固件是否是最新
			msglog "check data otaimg isnew [START]"
			check_otaimg_isnew ${otaimg_dir} update_data.md5
			if [ $? -eq 0 ]; then
				msglog "check data otaimg isnew [OK]"
				let validcnt++
				export otaimg_data_valid=1
			else
				msglog "check data otaimg isnew [ERROR]"
			fi
		else
			msglog "check data otaimg md5sum [ERROR]"
		fi
	else
		msglog "check data otaimg exist [error]"
	fi

	msglog "check root otaimg exist [start]"
	if [ -f ${otaimg_dir}/update_root.tar.gz ]&&[ -f ${otaimg_dir}/update_root.md5 ]; then
		#动态打开调试信息
		export msgdbg=1
		msglog "check root otaimg exist [OK]"
		#校验固件文件MD5
		msglog "check root otaimg md5sum [START]"
		check_otaimg_md5 ${otaimg_dir} update_root.tar.gz update_root.md5
		if [ $? -eq 0 ]; then
			msglog "check root otaimg md5sum [OK]"
			#校验固件是否是最新
			msglog "check root otaimg isnew [START]"
			check_otaimg_isnew ${otaimg_dir} update_root.md5
			if [ $? -eq 0 ]; then
				msglog "check root otaimg isnew [OK]"
				let validcnt++
				export otaimg_root_valid=1
			else
				msglog "check root otaimg isnew [ERROR]"
			fi
		else
			msglog "check root otaimg md5sum [ERROR]"
		fi
	else
		msglog "check root otaimg exist [error]"
	fi

	msglog "check rtfs otaimg exist [start]"
	if [ -f ${otaimg_dir}/update_rtfs.tar.gz ]&&[ -f ${otaimg_dir}/update_rtfs.md5 ]; then
		#动态打开调试信息
		export msgdbg=1
		msglog "check rtfs otaimg exist [OK]"
		#校验固件文件MD5
		msglog "check rtfs otaimg md5sum [START]"
		check_otaimg_md5 ${otaimg_dir} update_rtfs.tar.gz update_rtfs.md5
		if [ $? -eq 0 ]; then
			msglog "check rtfs otaimg md5sum [OK]"
			#校验固件是否是最新
			msglog "check rtfs otaimg isnew [START]"
			check_otaimg_isnew ${otaimg_dir} update_rtfs.md5
			if [ $? -eq 0 ]; then
				msglog "check rtfs otaimg isnew [OK]"
				let validcnt++
				export otaimg_rtfs_valid=1
			else
				msglog "check rtfs otaimg isnew [ERROR]"
			fi
		else
			msglog "check rtfs otaimg md5sum [ERROR]"
		fi
	else
		msglog "check rtfs otaimg exist [error]"
	fi

	if [ ${validcnt} -ne 0 ]; then
		#表示检测成功
		msglog "check otaimg files in ${otaimg_dir} [OK]"
		return 0
	fi
	#表示检测失败
	msglog "check otaimg files in ${otaimg_dir} [error]"
	return 1
}

try_otaimg_update()
{
	#通过ftype找到指定MTD分区
	dst_dev=$(eval echo '$'${otaimg_ftype}_dev)

	#通过ftype找到指定MNT点目录
	dst_mtp=$(eval echo '$'${otaimg_ftype}_mnt)

	#查找目标分区不存在
	if [ -z "${dst_dev}" ]; then
		msglog "otaimg check mtd parition: ${otaimg_ftype} [ERROR]"
		return 1
	fi
	msglog "otaimg check mtd partition: ${otaimg_ftype} [OK]"

	#查找目标目录不存在
	if [ -z "${dst_mtp}" ]; then
		msglog "otaimg check mtd mount folder: /${otaimg_ftype} [ERROR]"
		return 1
	fi
	msglog "otaimg check mtd mount folder: /${otaimg_ftype} [OK]"
	
	#挂载目标分区
	/bin/mount -o ${mntrwopt} ${dst_dev} ${dst_mtp} > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		msglog "otaimg mount parition to folder [ERROR]"
		return 1
	fi
	msglog "otaimg mount parition to folder [OK]"
	
	#通知更新开始
	msglog "otaimg update ${otaimg_ftype} [START]"
	notice_otaupdate_start
				
	#删除目标分区所有数据
	/bin/rm -rf  ${dst_mtp}/* > /dev/null 2>&1
	
	#解压升级文件到目标分区
	/bin/tar xzf ${otaimg_dir}/update_${otaimg_ftype}.tar.gz -C ${dst_mtp}
	if [ $? -eq 0 ]; then
		#解压成功
		msglog "otaimg update ${otaimg_ftype} [OK]"
		#记录固件文件的MD5
		cp -rf ${otaimg_dir}/update_${otaimg_ftype}.md5 ${otamgr_dir}/update_${otaimg_ftype}.md5.old
		#通知成功
		notice_otaupdate_finish
	else
		#解压失败
		msglog "otaimg update ${otaimg_ftype} [ERROR]"
		#通知失败
		notice_otaupdate_failed
		return 1
	fi
	return 0
}

#处理OTA升级事务
proc_otaimg_update()
{
	#检测固件文件是否有效
	check_otaimg_isvalid
	if [ $? -ne 0 ]; then
		#无效的固件文件
		return 1
	fi

	if [ ${otaimg_data_valid} -eq 1 ]; then		
		export otaimg_ftype=data
		try_otaimg_update
		if [ $? -ne 0 ]; then
			#OTA更新失败后不再继续更新后续固件文件
			return 1
		fi
	fi

	if [ ${otaimg_root_valid} -eq 1 ]; then		
		export otaimg_ftype=root
		try_otaimg_update
		if [ $? -ne 0 ]; then
			#OTA更新失败后不再继续更新后续固件文件
			return 1
		fi
	fi

	if [ ${otaimg_rtfs_valid} -eq 1 ]; then		
		export otaimg_ftype=rtfs
		try_otaimg_update
		if [ $? -ne 0 ]; then
			#OTA更新失败后不再继续更新后续固件文件
			return 1
		fi
	fi
	#OTA更新成功
	return 0
}

main_proc()
{
	/bin/mount -t proc     proc  /proc > /dev/null 2>&1
	/bin/mount -t sysfs    sysfs /sys  > /dev/null 2>&1
	/bin/mount -t devtmpfs none  /dev  > /dev/null 2>&1
	
	#初始化日志
	msginit
	exec < /dev/console > /dev/console 2>&1

	#OTA默认状态为空闲状态
	export OTA_STA_NOW="boot"

	#OTA默认升级是否立即重启
	export OTA_CAN_REBOOT=0

	#OTA状态文件默认目录
	export otamgr_dir=/vend

	#OTA状态文件名称
	export obmota_sta_file=".obmota_sta_file"

	#定义挂载目录
	export rtfs_mnt=/rtfs
	export root_mnt=/root
	export data_mnt=/data
	export vend_mnt=/vend

	#定义挂载选项
	export mntrwopt="rw,noatime,nodiratime,norelatime"

	mtd_total=$(/bin/cat /proc/mtd | /bin/grep mtd | /usr/bin/wc -l)
	for i in $(seq 0 ${mtd_total})
	do
    	if [ -f /sys/class/mtd/mtd${i}/name ]; then
			name=$(/bin/cat /sys/class/mtd/mtd${i}/name)
			if [ "xrtfs" = "x${name}" ]; then
				export rtfs_dev=/dev/mtdblock${i}
			elif [ "xroot" = "x${name}" ]; then
				export root_dev=/dev/mtdblock${i}
			elif [ "xdata" = "x${name}" ]; then
				export data_dev=/dev/mtdblock${i}
			elif [ "xvend" = "x${name}" ]; then
				export vend_dev=/dev/mtdblock${i}
			fi
		fi
	done

	if [ -z "$rtfs_dev" ]; then
		msglog "no rtfs device found [ERROR]"
		/sbin/getty -L ttyS0 115200 vt100 -n -l /bin/ash
		return 1
	fi
	
	if [ -z "$root_dev" ]; then
		msglog "no root device found [ERROR]"
	fi
	if [ -z "$data_dev" ]; then
		msglog "no data device found [ERROR]"
	fi

	wait_for_blkdev_ready ${rtfs_dev}
	if [ $? -eq 0 ]; then
		if [ -z "$vend_dev" ]; then
			#没有vend的MTD分区可挂载
			export is_vend_mnt=0
			msglog "no vend mtd found [ERROR]"
		else
			#尝试挂载vend的MTD分区到/vend
			#/bin/mount -o ${mntrwopt} ${vend_dev} ${vend_mnt} > /dev/null 2>&1
			mount_mtd_vend ${mntrwopt} ${dst_dev} ${dst_mtp} > /dev/null 2>&1
			if [ $? -eq 0 ]; then
				export is_vend_mnt=1
			else
				msglog "mount vend [ERROR]"
				export is_vend_mnt=0
			fi
		fi

		if [ ${is_vend_mnt} -eq 0 ]; then
			#当/vend没有挂载时
			export otamgr_dir="/fakev"
			#挂载fakev为临时文件系统
			fakev_mount
			msglog "obmota use ${otamgr_dir} as /vend for ota [WARN]"
		fi

		#第一次尝试从OTA状态文件读取OTA状态
		if [ ! -f /${otamgr_dir}/${obmota_sta_file} ]; then
			#将OTA状态设置为默认boot状态
			msglog "obmota create new state file: ${otamgr_dir}/${obmota_sta_file}"
			change_obmota_sta "boot"
		else
			msglog "obmota find state file ${otamgr_dir}/${obmota_sta_file}"
			export tmpsta=$(/bin/cat ${otamgr_dir}/${obmota_sta_file})
			is_net_ota_reboot=$(/bin/echo ${tmpsta}|/bin/grep "netotareboot")
			msglog "obmota get state ${tmpsta} from state file"
			if [ -n "${is_net_ota_reboot}" ]; then
				#将状态切换到网络升级状态
				change_obmota_sta "netotareboot"
			else
				#将OTA状态设置为默认boot状态
				change_obmota_sta "boot"
			fi
		fi

		if [ "x${OTA_STA_NOW}" = "xnetotareboot" ]&&[ ${is_vend_mnt} -eq 1 ]; then
				#处理网络升级
				#假设升级文件暂存在/vend分区
				export otaimg_dir=/vend
				proc_otaimg_update
				if [ $? -eq 0 ]; then
					msglog "obmota netotareboot update [DONE]"
				else
					msglog "obmota netotareboot update [FAILED]"
				fi
		else
			#系统每次开机都需要执行以下动作
			#>------------------------------------->bootckcusb
			#重新读取OTA状态以便检测U盘
			msglog "obmota before bootckcusb: ${OTA_STA_NOW}"

			#切换到bootckcusb状态
			change_obmota_sta "bootckcusb"

			#假设升级文件存储在U盘
			export otaimg_dir=/ual/udisk
			hotplug_check_ualmnt ${otaimg_dir}
			if [ $? -eq 0 ]; then
				proc_otaimg_update
				if [ $? -eq 0 ]; then
					msglog "obmota ${OTA_STA_NOW} update [DONE]"
				else
					msglog "obmota ${OTA_STA_NOW} update [failed]"
				fi
			else
				msglog "obmota ${OTA_STA_NOW} wait ${otaimg_dir} [TMO]"
			fi
			msglog "obmota after  bootckcusb: ${OTA_STA_NOW}"
			#>------------------------------------->bootckcsd
			#切换到bootckcsdc状态
			change_obmota_sta "bootckcsdc"

			#假设升级文件存储在SD卡
			export otaimg_dir=/ual/sdcard
			hotplug_check_ualmnt ${otaimg_dir}
			if [ $? -eq 0 ]; then
				proc_otaimg_update
				if [ $? -eq 0 ]; then
					msglog "obmota ${OTA_STA_NOW} update [DONE]"
				else
					msglog "obmota ${OTA_STA_NOW} update [failed]"
				fi
			else
				msglog "obmota ${OTA_STA_NOW} wait ${otaimg_dir} [TMO]"
			fi
			msglog "obmota after  bootckcsdc: ${OTA_STA_NOW}"
		fi

		msglog "obmota after all check: ${OTA_STA_NOW}"

		#切换到idle状态
		change_obmota_sta "idle"

		#卸载挂载内容需要注意顺序
		/bin/sync
		/bin/umount -l -r /rtfs > /dev/null 2>&1
		/bin/umount -l -r /data > /dev/null 2>&1
		/bin/umount -l -r /root > /dev/null 2>&1
		/bin/umount -l -r /ual/udisk  > /dev/null 2>&1
		/bin/umount -l -r /ual/sdcard > /dev/null 2>&1
		/bin/umount -l -r /ual > /dev/null  2>&1
		/bin/rm -rf       /ual > /dev/null  2>&1
		/bin/umount -l -r /vend > /dev/null 2>&1

		#/bin/mount -o ${mntrwopt} ${rtfs_dev} ${rtfs_mnt} > /dev/null 2>&1
		mount_mtd_rtfs ${mntrwopt} ${dst_dev} ${dst_mtp} > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			msglog "mount rtfs device: ${rtfs_dev} [ERROR]"
		else
			#cp -rf /etc/* ${rtfs_mnt}/etc/
			msglog "recover rtfs configs [DONE]"
			init_real_path=${rtfs_mnt}/sbin/init
			init_fake_path=/sbin/init
			if [ -x ${init_real_path} ]; then
				/bin/mount -n --move /proc /mnt/proc  > /dev/null 2>&1
				/bin/mount -n --move /sys  /mnt/sys   > /dev/null 2>&1
				/bin/mount -n --move /dev  /mnt/dev   > /dev/null 2>&1

				msglog "switch root to rtfs [START]"
				exec /sbin/switch_root ${rtfs_mnt} ${init_fake_path}
			else
				msglog "find init real path: ${init_real_path} [ERROR]"
			fi
		fi
	else
		msglog "check rtfs device: ${rtfs_dev} [ERROR]"
	fi
	msglog "system turn to fix mode [ERROR]"

	if [ ! -d /dev/pts ]; then
		mkdir /dev/pts
	fi
	/bin/mount -t devpts devpts  /dev/pts 
	
	/bin/hostname ${knfshostname} 

	/sbin/ifconfig lo    up             > /dev/null 2>&1
	/sbin/ifconfig eth0  192.168.1.100  > /dev/null 2>&1
	/sbin/route del default dev eth0    > /dev/null 2>&1
	/sbin/route add default gw  192.168.1.1
	/usr/sbin/telnetd 
	
	while [ 1 ]
	do
		/sbin/getty -L ttyS0 115200 vt100 -n /bin/login
		/bin/sleep 1
	done
	return 0
}

main_proc $@
